
#include <stdio.h>
#include <unistd.h>

#include "ohos_init.h"
#include "cmsis_os2.h"
#include "hi_io.h"
#include "hi_gpio.h"
#include "iot_gpio.h"

#include "iot_errno.h"
#include "iot_uart.h"
#include "hi_uart.h"

#define KEY1_PIN 11
#define KEY2_PIN 12

#define RED_LED_PIN 7
#define YELLOW_LED_PIN 14
#define GREEN_LED_PIN 13



#define GPIO_PIN_INIT    1
#define GPIO_PIN_UNINIT  0
static unsigned char g_gpioMap[HI_GPIO_IDX_MAX] = {0};
static unsigned char g_gpioInitNum = 0;

unsigned int IoTGpioInit(unsigned int id)
{
    if (id >= HI_GPIO_IDX_MAX) {
        return IOT_FAILURE;
    }

    if (g_gpioInitNum == 0) {
        (void)hi_gpio_init();
    }

    if (g_gpioMap[id] == GPIO_PIN_INIT) {
        return IOT_FAILURE;
    } else {
        g_gpioMap[id] = GPIO_PIN_INIT;
        g_gpioInitNum++;
    }

    return IOT_SUCCESS;
}




//定义流水灯的运动状态1：1为正向，-1为反向，0为停止
static int led_state = 1; 
//定义当前亮的灯的编号
static int led_current_bright  = 0; 

//按键1按下实现流水灯方向翻转
static void Key1_EXIT_Callback(char *arg)
{
    (void)arg;
    led_state = !led_state;
}

//按键2按下实现流水灯的开始与停止
static void Key2_EXIT_Callback(char *arg)
{
    (void)arg;
    led_state = -led_state;
}


static void LED_Task_Start(void)
{
    int led_pins[3] = {RED_LED_PIN, YELLOW_LED_PIN, GREEN_LED_PIN};
    while(1)
    {
        for(int i = 0; i < 3; i++)
        {
            if(i == led_current_bright)
            {
                //IoTGpioSetOutputVal(led_pins[i], 1);
                hi_gpio_set_ouput_val(led_pins[i], 1);

            }
            else
            {
                //IoTGpioSetOutputVal(led_pins[i], 0);
                hi_gpio_set_ouput_val(led_pins[i], 0);
            }
        }

        //下一个灯亮
        led_current_bright += led_state;

        //判断状态是否越界
        if(led_current_bright >= 3)
        {
            led_current_bright = 0;
        }
        else if(led_current_bright < 0)
        {
            led_current_bright = 2;
        }
        //延时500ms
        usleep(500*1000);
    }
}

static void ButtonDemoExEntry(void)
{
    IoTGpioInit(RED_LED_PIN);
    IoTGpioInit(YELLOW_LED_PIN);
    IoTGpioInit(GREEN_LED_PIN);

    //初始化LED灯
    hi_io_set_func(RED_LED_PIN, HI_IO_FUNC_GPIO_7_GPIO);
    hi_io_set_func(YELLOW_LED_PIN, HI_IO_FUNC_GPIO_14_GPIO);
    hi_io_set_func(GREEN_LED_PIN, HI_IO_FUNC_GPIO_13_GPIO);
/*
    IoTGpioSetDir(RED_LED_PIN, HI_GPIO_DIR_OUT);
    IoTGpioSetDir(YELLOW_LED_PIN, HI_GPIO_DIR_OUT);
    IoTGpioSetDir(GREEN_LED_PIN, HI_GPIO_DIR_OUT);
*/


    hi_gpio_set_dir(RED_LED_PIN, HI_GPIO_DIR_OUT);
    hi_gpio_set_dir(YELLOW_LED_PIN, HI_GPIO_DIR_OUT);
    hi_gpio_set_dir(GREEN_LED_PIN, HI_GPIO_DIR_OUT);
    

    //初始化KEY1按键，设置为下降沿触发中断，中断回调函数为Key1_EXIT_Callback
    IoTGpioInit(KEY1_PIN);
    hi_io_set_func(KEY1_PIN, HI_IO_FUNC_GPIO_11_GPIO);
    //IoTGpioSetDir(KEY1_PIN, HI_GPIO_DIR_IN);
    hi_gpio_set_dir(KEY1_PIN, HI_GPIO_DIR_IN);
    hi_io_set_pull(KEY1_PIN, HI_IO_PULL_UP);
    //IoTGpioRegisterIsrFunc(KEY1_PIN, IOT_INT_TYPE_EDGE, IOT_GPIO_EDGE_FALL_LEVEL_LOW, Key1_EXIT_Callback, NULL);
    hi_gpio_register_isr_function(KEY1_PIN, IOT_INT_TYPE_EDGE, IOT_GPIO_EDGE_FALL_LEVEL_LOW, Key1_EXIT_Callback, NULL);


    //初始化KEY2按键，设置为下降沿触发中断，中断回调函数为Key2_EXIT_Callback
    IoTGpioInit(KEY2_PIN);
    hi_io_set_func(KEY2_PIN, HI_IO_FUNC_GPIO_12_GPIO);
    //IoTGpioSetDir(KEY2_PIN, HI_GPIO_DIR_IN);
    hi_gpio_set_dir(KEY2_PIN, HI_GPIO_DIR_IN);
    hi_io_set_pull(KEY2_PIN, HI_IO_PULL_UP);
    //IoTGpioRegisterIsrFunc(KEY2_PIN, IOT_INT_TYPE_EDGE, IOT_GPIO_EDGE_FALL_LEVEL_LOW, Key2_EXIT_Callback, NULL);
    hi_gpio_register_isr_function(KEY2_PIN, IOT_INT_TYPE_EDGE, IOT_GPIO_EDGE_FALL_LEVEL_LOW, Key2_EXIT_Callback, NULL);

    //开启LED任务
    LED_Task_Start();
}

SYS_RUN(ButtonDemoExEntry);